import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpMessage;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.util.CharsetUtil;
import io.netty.util.concurrent.Future;

import java.net.URLDecoder;

public class NettyServerHttpHandler extends SimpleChannelInboundHandler<FullHttpMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpMessage message) throws Exception {
        if(message instanceof FullHttpRequest) {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) message;
            String content = fullHttpRequest.content().toString(CharsetUtil.UTF_8);
            if(content != null){
                content = URLDecoder.decode(content, CharsetUtil.UTF_8.name());
            }
            Future<String> future = ctx.executor().submit( new WorkerTask(content));
            future.addListener(new HttpFutureListener(ctx));
        }
    }


}
